<template>
  <!--<div class="image">
    <img src="https://ad.tanfan.cc/static/images/baoxiang.png" class="bx" @click="openAD"
         style="width: 65px;height: 65px" />
    <div class="djs" id="countdown"></div>
  </div>-->
</template>
<script>
import { Toast } from "vant";

export default {
  props: "",
  data() {
    return {
      time: 0,
      countdownElement:'',
      countdownInterval:0
    };
  },
  mounted() {
    let user_id = window.localStorage.getItem("uid") || "0";
    $http.post("test.is_auth_time", { user_id: user_id }, "loading").then(
      (response) => {
        let time = response.data.time;
        if (time > 0) {
            this.time = time
            this.startCountdown(time);
        }
      });
    },
  methods: {
    startCountdown() {
      // 创建用于显示倒计时的元素
      let that = this
      this.countdownElement = document.getElementById("countdown");

      // 更新倒计时的函数
      // 开始倒计时
      this.countdownInterval = setInterval(() =>{
          that.updateCountdown()
      }, 1000);
    }
    ,
    updateCountdown() {
      console.log('我在执行吗')
      // 计算剩余时间
      const now = new Date();
      const distance = this.time - now;
      // 判断倒计时是否结束
      if (distance <= 0) {
        clearInterval(this.countdownInterval);
        this.countdownElement.textContent = "点击领取宝箱";
        return;
      }
      console.log(distance,now)
      // 计算剩余时间
      const minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
      const seconds = Math.floor((distance % (1000 * 60)) / 1000);

      // 显示倒计时
      this.countdownElement.textContent = `${minutes}分 ${seconds}秒`;
    }
    ,
    openAD()
{
  let user_id = window.localStorage.getItem("uid") || "0";
  $http.post("test.is_auth_time", { user_id: user_id }, "loading").then(
    (response) => {
      let ad_type = response.data.ad_type;
      let time = response.data.time;
      if (time > 0) {
        this.time = time
        this.startCountdown(time);
      }
      if (ad_type > 0) {
        if (ad_type == 1) {
          console.log('执行激励')
          adutil.openAD(user_id);
        } else {
          console.log('执行插屏')
          adutil.openInterstitialAD(user_id);
        }
      }
    });
}
}
}
;
</script>
<style scoped lang="scss">
.djs {
  background: #f15353;
  border-radius: 20px;
  color: white;
}

.image {
  position: fixed;
  top: 50%;
  right: 10px;
  animation-name: jump;
  animation-duration: 2s;
  animation-iteration-count: infinite;
}
</style>